package com.pivotal.cloud.security.jwt;

import com.pivotal.cloud.crypto.cipher.CipherMetadata;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @className: com.pivotal.cloud.security.jwt.JwtAlgorithms
 * @projectName: 项目
 * @module: 项目-JwtAlgorithms类，主要位于模块
 * @content:
 * @author: Powered by Marklin
 * @datetime: 2025-06-21 20:41
 * @version:
 * @copyright: Copyright © 2018-2025 Systems Incorporated. All rights reserved.
 */
public class JwtAlgorithms {

	private static final Map<String, String> sigAlgs = new HashMap();

	private static final Map<Object, Object> javaToSigAlgs = new HashMap();

	private static final Map<String, String> keyAlgs = new HashMap();

	private static final Map<Object, Object> javaToKeyAlgs = new HashMap();

	public JwtAlgorithms() {
	}

	static String sigAlg(String javaName) {
		String alg = (String) javaToSigAlgs.get(javaName);
		if (alg == null) {
			throw new IllegalArgumentException("Invalid or unsupported signature algorithm: " + javaName);
		}
		else {
			return alg;
		}
	}

	static String keyEncryptionAlg(String javaName) {
		String alg = (String) javaToKeyAlgs.get(javaName);
		if (alg == null) {
			throw new IllegalArgumentException("Invalid or unsupported key encryption algorithm: " + javaName);
		}
		else {
			return alg;
		}
	}

	static String enc(CipherMetadata cipher) {
		if (!cipher.algorithm().equalsIgnoreCase("AES/CBC/PKCS5Padding")) {
			throw new IllegalArgumentException("Unknown or unsupported algorithm");
		}
		else if (cipher.keySize() == 128) {
			return "A128CBC";
		}
		else if (cipher.keySize() == 256) {
			return "A256CBC";
		}
		else {
			throw new IllegalArgumentException("Unsupported key size");
		}
	}

	static {
		sigAlgs.put("HS256", "HMACSHA256");
		sigAlgs.put("HS384", "HMACSHA384");
		sigAlgs.put("HS512", "HMACSHA512");
		sigAlgs.put("RS256", "SHA256withRSA");
		sigAlgs.put("RS512", "SHA512withRSA");
		keyAlgs.put("RSA1_5", "RSA/ECB/PKCS1Padding");
		Iterator var0 = sigAlgs.entrySet().iterator();

		Map.Entry e;
		while (var0.hasNext()) {
			e = (Map.Entry) var0.next();
			javaToSigAlgs.put(e.getValue(), e.getKey());
		}

		var0 = keyAlgs.entrySet().iterator();

		while (var0.hasNext()) {
			e = (Map.Entry) var0.next();
			javaToKeyAlgs.put(e.getValue(), e.getKey());
		}

	}

}
